#! /usr/bin/env python
# -*- coding: utf-8 -*-

from playwright.sync_api import Page
from ui_automation_test.common.base_page import BasePage
from ui_automation_test.locators.dualPrevention.hiddenDangerTreatment.hazardManagement.loc_hidden_danger_add import LocAddHiddenDanger
from ui_automation_test.locators.dualPrevention.hiddenDangerTreatment.hazardManagement.loc_hidden_danger_issue import LocIssueHiddenDanger
from ui_automation_test.locators.dualPrevention.hiddenDangerTreatment.hazardManagement.loc_hidden_danger_rectification import LocRectificationHiddenDanger
from ui_automation_test.locators.dualPrevention.hiddenDangerTreatment.hazardManagement.loc_hidden_danger_extension import LocExtensionHiddenDanger
from ui_automation_test.locators.dualPrevention.hiddenDangerTreatment.hazardManagement.loc_hidden_danger_search import LocSearchHiddenDanger
from ui_automation_test.locators.dualPrevention.hiddenDangerTreatment.hazardManagement.loc_hidden_danger_view import LocViewHiddenDanger
from ui_automation_test.locators.dualPrevention.hiddenDangerTreatment.hazardManagement.loc_hidden_danger_acceptance import LocAcceptanceHiddenDanger
from ui_automation_test.locators.dualPrevention.hiddenDangerTreatment.hazardManagement.loc_hidden_danger_delay_approval import LocDelayApprovalHiddenDanger
from ui_automation_test.common.logger import logger


class HazardManagement(BasePage):   

    def __init__(self, page: Page):
        super().__init__(page)
        self.switch_menu('双重预防机制')
        self.switch_menu('隐患治理闭环管理')
        self.switch_menu('隐患管理（企业）')       
    

    def add_hidden_danger(self, hidden_danger_source, hidden_danger_name, risk_object, risk_unit, major_hidden_danger, hidden_danger_type, hidden_danger_treatment_type, hidden_danger_description, file_path):
        '''
        新增隐患
        :param hidden_danger_source: 隐患来源
        :param hidden_danger_name: 隐患名称
        :param risk_object: 所属风险对象
        :param risk_unit: 所属风险单元
        :param major_hidden_danger: 是否存在任一重大隐患情形
        :param hidden_danger_type: 隐患类型
        :param hidden_danger_description: 隐患描述
        :param file_path: 隐患图片路径
        :param hidden_danger_treatment_type: 隐患治理类型
        '''
        logger.info('点击新增按钮')    
        self.page.get_by_role('button', name='新增').first.click()
        logger.info('选择隐患来源')
        self.select(LocAddHiddenDanger.loc_hidden_danger_source, hidden_danger_source)
        logger.info('输入隐患名称')
        self.fill(LocAddHiddenDanger.loc_hidden_danger_name, hidden_danger_name)
        logger.info('选择所属风险对象')
        self.select(LocAddHiddenDanger.loc_risk_object,risk_object)
        logger.info('选择所属风险单元')
        self.select(LocAddHiddenDanger.loc_risk_unit, risk_unit)
        logger.info('选择是否存在任一重大隐患情形')
        self.select(LocAddHiddenDanger.loc_major_hidden_danger, major_hidden_danger)
        logger.info('选择隐患类型')
        self.select(LocAddHiddenDanger.loc_hidden_danger_type, hidden_danger_type)
        logger.info('输入隐患描述')
        self.fill(LocAddHiddenDanger.loc_hidden_danger_description, hidden_danger_description)
        # 鼠标滚动到文件上传按钮
        self.page.mouse.wheel(0, 1000)
        self.page.wait_for_timeout(500)
        logger.info('上传隐患图片')
        self.upload_file(file_path, 0)
        logger.info('选择隐患治理类型')
        self.check_complex(LocAddHiddenDanger.loc_hidden_danger_treatment_type, hidden_danger_treatment_type)
        logger.info('点击提交按钮')
        self.click(LocAddHiddenDanger.loc_submit)
    
    def issue_hidden_danger(self, hidden_danger_name, period, dept, responsible, requirement):
        '''
        下发整改通知
        :param hidden_danger_name: 隐患名称
        :param hidden_danger_treatment_period: 隐患治理期限
        :param hidden_danger_treatment_requirement: 整改要求/预案
        '''

        # 下发新增隐患的整改通知
        logger.info('获取新增隐患的下发整改通知按钮位置')
        self.click_table_button_by_row(0, 17,'下发整改通知',hidden_danger_name)
        logger.info('选择隐患治理期限')
        self.fill(LocIssueHiddenDanger.loc_hidden_danger_treatment_period, period)
        logger.info('选择整改责任人')
        self.click(LocIssueHiddenDanger.loc_hidden_danger_treatment_responsible)
        self.page.wait_for_timeout(500)
        self.page.get_by_role('tooltip', name='').get_by_role('menuitem', name=dept).click()
        self.page.wait_for_timeout(500)
        self.page.get_by_role('tooltip', name='').get_by_role('menuitem', name=responsible).press('Enter')
        self.page.wait_for_timeout(500)
        logger.info('输入整改要求/预案')
        self.fill(LocIssueHiddenDanger.loc_hidden_danger_treatment_requirement, requirement)
        logger.info('点击确定按钮')
        self.click(LocIssueHiddenDanger.loc_submit)

    def extension_hidden_danger(self, hidden_danger_name, extension_period, extension_reason):
        '''
        隐患延期申请
        :param hidden_danger_name: 隐患名称
        :param extension_period: 延期期限
        :param extension_reason: 延期原因
        :param risk_object: 所属风险对象
        :param risk_unit: 所属风险单元
        :param rectification_status: 整改状态
        '''
        # 新增隐患延期申请
        logger.info('查询出要待整改的隐患')
        self.search_hidden_danger(hidden_danger_name, risk_object=None, risk_unit=None, rectification_status=None)
        logger.info('点击延期申请按钮')
        self.click_table_button_by_row(0, 17, '延期申请', hidden_danger_name)
        logger.info('选择延期期限')
        self.fill(LocExtensionHiddenDanger.loc_hidden_danger_extension_period, extension_period)
        logger.info('输入延期原因')
        self.fill(LocExtensionHiddenDanger.loc_hidden_danger_extension_reason, extension_reason)
        self.page.wait_for_timeout(500)
        logger.info('点击确定按钮')
        self.click(LocExtensionHiddenDanger.loc_submit)

    def delay_approval_hidden_danger(self, hidden_danger_name, delay_approval_result, delay_approval_situation):
        '''
        隐患延期审批
        :param hidden_danger_name: 隐患名称
        :param delay_approval_result: 审批结果
        :param delay_approval_situation: 审批意见
        '''
        # 新增隐患延期审批
        self.search_hidden_danger(hidden_danger_name, risk_object=None, risk_unit=None, rectification_status=None)
        logger.info('点击延期审批按钮')
        self.click_table_button_by_row(0, 17, '延期审批', hidden_danger_name)
        logger.info('选择审批结果')
        if delay_approval_result == '通过':
            self.check_complex(LocDelayApprovalHiddenDanger.loc_hidden_danger_delay_approval_result, '通过')
        elif delay_approval_result == '不通过':
            self.check_complex(LocDelayApprovalHiddenDanger.loc_hidden_danger_delay_approval_result, '不通过')
        logger.info('输入审批意见')
        self.fill(LocDelayApprovalHiddenDanger.loc_hidden_danger_delay_approval_situation, delay_approval_situation)
        self.page.wait_for_timeout(500)
        logger.info('点击确定按钮')
        self.click(LocDelayApprovalHiddenDanger.loc_submit)

    def rectification_hidden_danger(self, hidden_danger_name, rectification_date, rectification_fund, rectification_reason, rectification_measure, file_path):        
        '''
        隐患整改
        :param hidden_danger_name: 隐患名称
        :param rectification_date: 整改日期
        :param rectification_fund: 整改资金
        :param rectification_reason: 原因分析
        :param rectification_measure: 控制措施
        :param rectification_image: 治理后图片
        '''
        # 新增隐患整改
        self.search_hidden_danger(hidden_danger_name, risk_object=None, risk_unit=None, rectification_status=None)
        self.page.wait_for_timeout(500)
        logger.info('点击整改按钮')
        self.click_table_button_by_row(0, 17, '整改', hidden_danger_name)
        logger.info('选择整改日期')   
        self.fill(LocRectificationHiddenDanger.loc_hidden_danger_rectification_date, rectification_date)
        logger.info('输入整改资金')
        self.fill_number(LocRectificationHiddenDanger.loc_hidden_danger_rectification_fund, rectification_fund)
        logger.info('输入原因分析')
        self.fill(LocRectificationHiddenDanger.loc_hidden_danger_rectification_reason, rectification_reason)
        logger.info('输入控制措施')
        self.fill(LocRectificationHiddenDanger.loc_hidden_danger_rectification_measure, rectification_measure)
        logger.info('上传治理后图片')
        self.upload_file(file_path, 0)
        logger.info('点击确定按钮')
        self.click(LocRectificationHiddenDanger.loc_submit)
        self.page.wait_for_timeout(1000)


    def acceptance_hidden_danger(self, hidden_danger_name, acceptance_result, acceptance_situation):
        '''
        隐患验收
        :param hidden_danger_name: 隐患名称
        '''
        # 新增隐患验收
        self.search_hidden_danger(hidden_danger_name, risk_object=None, risk_unit=None, rectification_status=None)
        self.page.wait_for_timeout(500)
        logger.info('点击验收按钮')
        self.click_table_button_by_row(0, 17, '验收', hidden_danger_name)
        logger.info('选择验收结果')
        if acceptance_result == '通过':
            self.check_complex(LocAcceptanceHiddenDanger.loc_hidden_danger_acceptance_result, '通过')
        elif acceptance_result == '不通过':
            self.check_complex(LocAcceptanceHiddenDanger.loc_hidden_danger_acceptance_result, '不通过')
        logger.info('输入验收情况')
        self.fill(LocAcceptanceHiddenDanger.loc_hidden_danger_acceptance_situation, acceptance_situation)
        logger.info('点击确定按钮')
        self.click(LocAcceptanceHiddenDanger.loc_submit)
        self.page.wait_for_timeout(1000)

    def search_hidden_danger(self, hidden_danger_name=None, risk_object=None, risk_unit=None, rectification_status=None):
        '''
        :param hidden_danger_name: 隐患名称
        :param risk_object: 所属风险对象
        :param risk_unit: 所属风险单元
        :param rectification_status: 整改状态
        '''
        logger.info('选择整改状态')
        if rectification_status == '待下发':
            self.check_complex(LocSearchHiddenDanger.loc_rectification_status, '待下发')
        elif rectification_status == '待整改':
            self.check_complex(LocSearchHiddenDanger.loc_rectification_status, '待整改')
        elif rectification_status == '延期审批中':
            self.check_complex(LocSearchHiddenDanger.loc_rectification_status, '延期审批中')
        elif rectification_status == '延期整改中':
            self.check_complex(LocSearchHiddenDanger.loc_rectification_status, '延期整改中')
        elif rectification_status == '逾期未整改':
            self.check_complex(LocSearchHiddenDanger.loc_rectification_status, '逾期未整改')
        elif rectification_status == '验收中':
            self.check_complex(LocSearchHiddenDanger.loc_rectification_status, '验收中')
        elif rectification_status == '已验收':
            self.check_complex(LocSearchHiddenDanger.loc_rectification_status, '已验收')
        elif rectification_status == '全部':
            self.check_complex(LocSearchHiddenDanger.loc_rectification_status, '全部')
        logger.info('输入隐患名称')
        if hidden_danger_name:
            self.fill(LocSearchHiddenDanger.loc_hidden_danger_name, hidden_danger_name)
        logger.info('选择所属风险对象')
        if risk_object:
            self.select(LocSearchHiddenDanger.loc_risk_object, risk_object)
        logger.info('选择所属风险单元')
        if risk_unit:
            self.select(LocSearchHiddenDanger.loc_risk_unit, risk_unit)
        logger.info('点击查询按钮')
        self.click(LocSearchHiddenDanger.loc_search_button)

    def view_hidden_danger(self, hidden_danger_name):
        '''
        查看隐患
        :param hidden_danger_name: 隐患名称
        '''
        self.search_hidden_danger(hidden_danger_name, risk_object=None, risk_unit=None, rectification_status=None)
        logger.info('点击查看按钮')
        self.click_table_button_by_row(0, 17, '查看', hidden_danger_name)
        self.page.wait_for_timeout(1000)